from django.db import models

from apps.user.models import User


class Category(models.Model):
    """
    文章分类表
    """
    nid = models.AutoField(primary_key=True)
    title = models.CharField(verbose_name='分类标题',max_length=32)


    class Meta:
        db_table = 'Category'
        verbose_name = '文章分类'
        verbose_name_plural = verbose_name
    def __str__(self):
        return self.title

class Article(models.Model):
    """
    文章表
    """
    nid = models.AutoField(primary_key=True)
    title = models.CharField(max_length=50, verbose_name='文章标题')
    create_time = models.DateTimeField(verbose_name='创建时间',auto_now_add=True)
    content = models.TextField(verbose_name='文章内容')
    comment_count = models.IntegerField(verbose_name='评论数',default=0)
    up_count = models.IntegerField(verbose_name='点赞数',default=0)
    user = models.ForeignKey(User, verbose_name='作者',on_delete=models.CASCADE)
    category = models.ForeignKey(Category, to_field='nid',null=True,verbose_name='文章分类',on_delete=models.SET_NULL)

    class Meta:
        db_table = 'Article'
        verbose_name = '文章详情'
        verbose_name_plural = verbose_name
    def __str__(self):
        return self.title


class ArticleUpDown(models.Model):
    """
    点赞表
    """
    nid = models.AutoField(primary_key=True)
    user = models.ForeignKey(User, null=True, on_delete=models.CASCADE)
    article = models.ForeignKey(Article, null=True, on_delete=models.CASCADE)
    is_up = models.BooleanField(default=True)
    class Meta:
        db_table = 'UpDown'
        verbose_name = '点赞'
        verbose_name_plural = verbose_name
    def __str__(self):
        return self.nid

class Comment(models.Model):
    """
    评论表
    """
    nid = models.AutoField(primary_key=True)
    article = models.ForeignKey(Article, verbose_name='评论文章',to_field='nid',on_delete=models.CASCADE)
    user = models.ForeignKey(User, verbose_name='评论者', on_delete=models.CASCADE)
    content = models.CharField(verbose_name='评论内容', max_length=25)
    class Meta:
        db_table = 'Comment'
        verbose_name = '评论'
        verbose_name_plural = verbose_name
    def __str__(self):
        return self.nid